package Manager.Backup;

import java.awt.event.*;
import java.io.IOException;
import java.sql.*;

import javax.swing.*;

import Database.*;
import Exception.CloseConnectionException;
import Exception.LoadDriverException;
import Exception.OpenConnectionException;
import General.*;

public class PanelBackup extends JPanel
{
	PanelBackupToFile panelBackupToFile;
	JButton buttonBackup;
	
	private Connection connection;
	private CallDB callDB;
	
	public PanelBackup(CallDB callDB, Connection connection) throws OpenConnectionException, LoadDriverException
	{
		setLayout(null);
		
		this.connection = connection;
		this.callDB = callDB;
		
		createComponent();
		showComponent();
		addListeners();
	}
	
	private void createComponent() throws OpenConnectionException, LoadDriverException
	{
		// First line
		panelBackupToFile = new PanelBackupToFile();
		panelBackupToFile.setBounds(20,80,575,180);
		
		buttonBackup = new JButton("Backup !");
		buttonBackup.setBounds(620,100,150,155);
	}
	
	private void showComponent()
	{
		add(panelBackupToFile);
		add(buttonBackup);
		
		GraphicsSetting.setBackground(this, "image/Bg-Main.jpg", 0, 0, 800, 600);
	}
	
	private void addListeners()
	{
		MyActionListener listenerAction = new MyActionListener();
		buttonBackup.addActionListener(listenerAction);
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonBackup)
					panelBackupToFile.getBackup().backupDB();

				callDB.closeConnection(connection);
			}
			catch(CloseConnectionException | InterruptedException | IOException error)
			{
				TreatError.showError(error);
			}
		}
	}
}
